/**
 * ------------------------------------------------------------------------------------------
 * Copyright (c): Company, 2012 This software is the
 * proprietary information of FHWS, Würzburg All Right Reserved.
 * ------------------------------------------------------------------------------------------
 *
 *
 * @project: Vorlesungplan
 * @copyright FHWS 2012
 * @link       http://mussnocheingeragenwerden.de
 * @package    tt.model
 *
 * SVN revision information:
 *
 * @version $Rev$:
 * @author $Author$:
 * @date $Date$:
 */

package tt.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "semester")
@XmlRootElement
@NamedQueries(
{
   @NamedQuery(name = "Semester.findAll", query = "SELECT s FROM Semester s"),
   @NamedQuery(name = "Semester.findByIdsemester", query = "SELECT s FROM Semester s WHERE s.idsemester = :idsemester order by s.number"),
   @NamedQuery(name = "Semester.findByNumber", query = "SELECT s FROM Semester s WHERE s.number = :number")
})
public class Semester implements Serializable, Comparable<Semester>
{

   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "idsemester")
   private Integer idsemester;
   @Basic(optional = false)
   @NotNull
   @Column(name = "number")
   private int number;
   @OneToMany(mappedBy = "semester")
   private Collection<Lecture> lectureCollection;
   @JoinColumn(name = "courseofstudy", referencedColumnName = "idcourse")
   @ManyToOne(optional = false)
   private Courseofstudy courseofstudy;

   public Semester()
   {
   }

   public Semester(Integer idsemester)
   {
      this.idsemester = idsemester;
   }

   public Semester(Integer idsemester, int number)
   {
      this.idsemester = idsemester;
      this.number = number;
   }

   public Integer getIdsemester()
   {
      return idsemester;
   }

   public void setIdsemester(Integer idsemester)
   {
      this.idsemester = idsemester;
   }

   public int getNumber()
   {
      return number;
   }

   public void setNumber(int number)
   {
      this.number = number;
   }

   @XmlTransient
   public Collection<Lecture> getLectureCollection()
   {
      Collections.sort((List<Lecture>) lectureCollection);
      return lectureCollection;
   }

   public void setLectureCollection(Collection<Lecture> lectureCollection)
   {
      this.lectureCollection = lectureCollection;
   }

   public Courseofstudy getCourseofstudy()
   {
      return courseofstudy;
   }

   public void setCourseofstudy(Courseofstudy courseofstudy)
   {
      this.courseofstudy = courseofstudy;
   }

   @Override
   public int hashCode()
   {
      int hash = 0;
      hash += (idsemester != null ? idsemester.hashCode() : 0);
      return hash;
   }

   @Override
   public boolean equals(Object object)
   {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Semester))
      {
         return false;
      }
      Semester other = (Semester) object;
      if ((this.idsemester == null && other.idsemester != null) || (this.idsemester != null && !this.idsemester.equals(other.idsemester)))
      {
         return false;
      }
      return true;
   }

   @Override
   public String toString()
   {
      return "tt.model.Semester[ idsemester=" + idsemester + " ]";
   }

   public int compareTo(Semester o)
   {
      if (number > o.getNumber())
      {
         return 1;
      } else if (number == o.getNumber())
      {
         return 0;
      } else
      {
         return -1;
      }

   }//endmethod
}
